package com.beta.backend.gateway.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.time.Instant;
import java.util.Date;

/**
 * 仅在本包使用,因为不能导入cloud的包，所以不能用commonReponse
 *
 * @author wikili
 */
public final class ResponseUtils {

    private ResponseUtils(){}

    @Data
    private static class ResponseResult {
        private Long timeStamp = Date.from(Instant.now()).getTime();

        ResponseResult(String errorCode, String errorMessage) {
            this.errorCode = errorCode;
            this.errorMessage = errorMessage;
        }

        private String results;

        private String errorCode;

        private String errorMessage;
    }

    public static String generateResponse(ObjectMapper objectMapper, HttpStatus status) {
        String response = status.name();
        try {
            response = objectMapper.writeValueAsString(new ResponseResult(String.valueOf(status.value()), status.name()));
        } catch (Exception e) {
            //do nothing
        }
        return response;
    }

}
